﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SetRegionUC
{
    /// <summary>
    /// Set region user control
    /// </summary>
    public partial class SetRegionForm : DevComponents.DotNetBar.Office2007Form
    {
        #region Attributs and Getter/Setter
        private FVIL.Forms.CFviImageView m_imageView;
        public EventHandler eventCloseForm;
        public EventHandler eventSaveForm;
        private RectRegion m_rectRegion;

        public RectRegion RectRegion
        {
            get { return m_rectRegion; }
            set { m_rectRegion = value; }
        }

        public FVIL.Forms.CFviImageView ImageView
        {
            get { return m_imageView; }
            set { m_imageView = value; }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        public SetRegionForm()
        {
            InitializeComponent();
        }

        #region Logic functions
        /// <summary>
        /// Initialize parameters
        /// </summary>
        /// <param name="imageView"></param>
        public void setImageView(FVIL.Forms.CFviImageView imageView)
        {
            m_imageView = imageView;
            m_imageView.Display.OverlayPatternRegist.Active = true;
            m_imageView.Refresh();
            m_imageView.Paint += new PaintEventHandler(OnPaintImageView);
        }

        /// <summary>
        /// Draw on image view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPaintImageView(object sender, PaintEventArgs e)
        {
            m_imageView.Display.OverlayPatternRegist.CenterMark.X =
                (double)m_imageView.Display.OverlayPatternRegist.RegistRect.Left;
            m_imageView.Display.OverlayPatternRegist.CenterMark.Y =
                (double)m_imageView.Display.OverlayPatternRegist.RegistRect.Top;
            m_rectRegion.TopX = m_imageView.Display.OverlayPatternRegist.RegistRect.Left.ToString();
            m_rectRegion.TopY = m_imageView.Display.OverlayPatternRegist.RegistRect.Top.ToString();
            m_rectRegion.BottomX = m_imageView.Display.OverlayPatternRegist.RegistRect.Right.ToString();
            m_rectRegion.BottomY = m_imageView.Display.OverlayPatternRegist.RegistRect.Bottom.ToString();
            pgSetRegion.SelectedObject = m_rectRegion;
        }
        #endregion

        #region Events on form
        private void SetRegionForm_Load(object sender, EventArgs e)
        {
            m_rectRegion = new SetRegionUC.RectRegion("0", "0", "100", "100");
            pgSetRegion.SelectedObject = m_rectRegion;
            timerSetRegion.Start();
        }

        private void SetRegionForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            OnCloseForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //OnCloseForm();
            this.Close();
        }

        public virtual void OnCloseForm()
        {
            EventHandler handler = eventCloseForm;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
            hideRegistRegion();
        }

        private void hideRegistRegion()
        {
            m_imageView.Display.OverlayPatternRegist.Active = false;
            m_imageView.Paint -= new PaintEventHandler(OnPaintImageView);
            m_imageView.Refresh();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OnSaveForm();
            this.Close();
        }

        public virtual void OnSaveForm()
        {
            EventHandler handler = eventSaveForm;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
            hideRegistRegion();
        }

        /// <summary>
        /// Hide set region form when cursor leave form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerSetRegion_Tick(object sender, EventArgs e)
        {
            // Compute rectangle bouding form
            Rectangle rect = new Rectangle();
            rect.Location = this.PointToScreen(this.Location);
            rect.Width = this.Width;
            rect.Height = this.Height;
            // Check if cursor on form rectangle
            bool isIn = rect.Contains(this.PointToScreen(Cursor.Position));
            this.Opacity = isIn ? 0.99 : 0.3;
        }

        private void pgSetRegion_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (!Number.isNumberBoundedBelow(e.ChangedItem.Value.ToString(), 0)
                || (Convert.ToInt32(e.ChangedItem.Value) > m_imageView.Image.HorzSize
                & (e.ChangedItem.Label == "TopX" || e.ChangedItem.Label == "BottomX"))
                || (Convert.ToInt32(e.ChangedItem.Value) > m_imageView.Image.VertSize
                & (e.ChangedItem.Label == "TopY" || e.ChangedItem.Label == "BottomY")))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(pgSetRegion.SelectedObject, e.OldValue);
                return;
            }
            updateOverlayRegist();
        }

        /// <summary>
        /// Update overlay region from values in grid property
        /// </summary>
        private void updateOverlayRegist()
        {
            m_imageView.Display.OverlayPatternRegist.RegistRect.Left = Convert.ToDouble(m_rectRegion.TopX);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Top = Convert.ToDouble(m_rectRegion.TopY);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Right = Convert.ToDouble
                    (m_rectRegion.BottomX);
            m_imageView.Display.OverlayPatternRegist.RegistRect.Bottom = Convert.ToDouble
                   (m_rectRegion.BottomY);
            m_imageView.Display.OverlayPatternRegist.CenterMark.Y =
                m_imageView.Display.OverlayPatternRegist.RegistRect.Top;
            m_imageView.Display.OverlayPatternRegist.CenterMark.X =
                m_imageView.Display.OverlayPatternRegist.RegistRect.Left;
            m_imageView.Refresh();
        }

        #endregion
    }

    /// <summary>
    /// To create object for grid property
    /// </summary>
    public class RectRegion
    {
        private string m_topX;
        private string m_topY;
        private string m_bottomX;
        private string m_bottomY;

        public string TopX
        {
            get { return m_topX; }
            set { m_topX = value; }
        }

        public string TopY
        {
            get { return m_topY; }
            set { m_topY = value; }
        }

        public string BottomX
        {
            get { return m_bottomX; }
            set { m_bottomX = value; }
        }

        public string BottomY
        {
            get { return m_bottomY; }
            set { m_bottomY = value; }
        }

        public RectRegion(string topX, string topY, string bottomX, string bottomY)
        {
            m_topX = topX;
            m_topY = topY;
            m_bottomX = bottomX;
            m_bottomY = bottomY;
        }
    }
}
